{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 动态数组\n",
    "# 初始化一个空列表\n",
    "nums = []\n",
    "\n",
    "# 初始化一个包含元素 1, 3, 5 的列表\n",
    "nums = [1, 3, 5]\n",
    "\n",
    "# 初始化大小为 n，元素都为0的列表\n",
    "n = 10\n",
    "nums = [0] * n\n",
    "\n",
    "# 二维列表，m 行 n 列，元素都为 1\n",
    "m, n = 3, 4\n",
    "matrix = [[1] * n for _ in range(m)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表常用的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [0] * 10\n",
    "\n",
    "# 输出：False\n",
    "print(len(nums) == 0)\n",
    "# 输出：10\n",
    "print(len(nums))\n",
    "\n",
    "# 在列表尾部插入一个元素 20\n",
    "nums.append(20)\n",
    "# 输出：11\n",
    "print(len(nums))\n",
    "\n",
    "# 得到列表最后一个元素，输出：20\n",
    "print(nums[-1])\n",
    "\n",
    "# 删除列表的最后一个元素\n",
    "nums.pop()\n",
    "# 输出：10\n",
    "print(len(nums))\n",
    "\n",
    "# 索引访问与修改\n",
    "nums[0] = 11\n",
    "# 输出：11\n",
    "print(nums[0])\n",
    "\n",
    "# 在索引 3 处插入一个元素 99\n",
    "nums.insert(3, 99)\n",
    "\n",
    "# 删除索引 2 处的元素\n",
    "nums.pop(2)\n",
    "\n",
    "# 交换 nums[0] 和 nums[1]\n",
    "nums[0], nums[1] = nums[1], nums[0]\n",
    "\n",
    "# 遍历列表\n",
    "# 输出示例：\n",
    "# 0 11 99 0 0 0 0 0 0 0\n",
    "for num in nums:\n",
    "    print(num, end=\" \")\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类型提醒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List, Dict\n",
    "\n",
    "def process_data(data: List[Dict[str, str]]) -> None:\n",
    "    \"\"\"\n",
    "    Processes a list of dictionaries containing string values.\n",
    "  \n",
    "    Args:\n",
    "        data (List[Dict[str, str]]): A list of dictionaries with string keys and values.\n",
    "    \"\"\"\n",
    "    # 函数的具体实现...\n",
    "    pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List  \n",
    "  \n",
    "def sum_numbers(numbers: List[int]) -> int:  \n",
    "    return sum(numbers)  \n",
    "  \n",
    "# 正确使用  \n",
    "result = sum_numbers([1, 2, 3, 4])  \n",
    "print(result)  # 输出 10  \n",
    "  \n",
    "# 如果传入错误类型的列表，类型检查器会发出警告（如果你使用了如mypy这样的类型检查器）  \n",
    "# 错误使用（在静态类型检查时会报错，但运行时不会有问题，因为Python是动态类型的）  \n",
    "wrong_result = sum_numbers(['1', '2', '3'])  # 类型检查器会报错，因为列表中的元素不是整数\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "science39"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
